package zw_601_700.zw_643_子数组最大平均数I;

class Solution {

    public static void main(String[] args) {
        int[] nums = {1, 12, 3};
        int k = 2;
        double maxAverage = findMaxAverage(nums, k);
        System.out.println(maxAverage);
    }

    public static double findMaxAverage(int[] nums, int k) {
        int sum = 0;
        int n = nums.length;
        for (int i = 0; i < k; i++) {
            sum += nums[i];//保存第一个k
        }
        int maxSum = sum;
        for (int i = k; i < n; i++) {
            sum = sum - nums[i - k] + nums[i];//减去最后一个,加上头部一个
            maxSum = Math.max(maxSum, sum);//取得最大值
        }
        return 1.0 * maxSum / k;
    }
}
